<template>
	<view class="page">
		<view class="input-wrap">
			<view class="input-item border-b">
				<view class="input-label">姓名:</view>
				<view class="input">
					<input type="text" class="add-inp" v-if="inpSbm != ''" v-model="driver.realname" />
					<input type="text" class="add-inp" v-if="inpSbm == ''" v-model="driver.realname"
						placeholder="请输入您的真实姓名">
				</view>
			</view>
			<view class="input-item">
				<view class="input-label">手机号:</view>
				<view class="input">
					<input type="text" class="add-inp" v-model="driver.mobile" v-if="inpSbm != ''" />
					<input type="text" class="add-inp" v-model="driver.mobile" v-else placeholder="请输入您的手机号码">
				</view>
			</view>
			<view class="input-item">
				<view class="input-label">年龄:</view>
				<view class="input">
					<input type="number" class="add-inp" v-model="driver.age" v-if="inpSbm != ''" />
					<input type="number" class="add-inp" v-else placeholder="请输入年龄" v-model="driver.age" />
				</view>
			</view>
			<view class="input-item">
				<view class="input-label">日薪:</view>
				<view class="input">
					<input type="text" class="add-inp" v-model="driver.pay" v-if="inpSbm != ''" />
					<input type="text" class="add-inp" v-else placeholder="请输入报酬" v-model="driver.pay" />
					<text class="k">元</text>
				</view>
				
			</view>
			<view class="input-item">
				<view class="input-label">驾照类型:</view>
				<!-- <view class="input">
					<input type="text" v-model="inpSbm.type_name" class="add-inp" v-if="status == 0 &&status == 1" />
					<input type="text" v-if="status != 0 &&status != 1" class="add-inp" placeholder="请选择" v-model="driverType"
						disabled="" />
					<picker v-if="status != 0 &&status != 1" @change="seleceType" range-key="type_name" :range="selectType"
						:value="index">
						<image src="../../../static/select.png"></image>
					</picker>
				</view> -->
				<view class="input">
					<input type="text" v-model="driver.type_name" class="add-inp" v-if="inpSbm!=''" />
					<input type="text" v-if="inpSbm == ''" class="add-inp" placeholder="请选择" v-model="driver.type_name"
						disabled="" />
					<picker v-if="status != 0 &&status != 1" @change="seleceType" range-key="type_name" :range="selectType"
						:value="index">
						<image src="../../../static/select.png"></image>
					</picker>
				</view>
			</view>
			<view class="input-item">
				<view class="input-label">驾龄:</view>
				<!-- <view class="input">
					<input type="text" v-model="inpSbm.age_name" class="add-inp" v-if="status == 0 &&status == 1" />
					<input type="text" v-if="status != 0 &&status != 1" class="add-inp" disabled="" placeholder="请选择"
						v-model="driverAge" />
					<picker v-if="status != 0 &&status != 1" @change="seleceYears" range-key="driver_age" :range="yearData"
						:value="index">
						<image src="../../../static/select.png"></image>
					</picker>
				</view> -->
				<view class="input">
					<input type="text" v-model="driver.age_name" class="add-inp" v-if="inpSbm != ''" />
					<input type="text" v-if="inpSbm == ''" class="add-inp" disabled="" placeholder="请选择"
						v-model="driver.age_name" />
					<picker v-if="status != 0 &&status != 1" @change="seleceYears" range-key="driver_age" :range="yearData"
						:value="index">
						<image src="../../../static/select.png"></image>
					</picker>
				</view>
			</view>
			<view class="input-item">
				<view class="input-label">性别:</view>
				<!-- <view class="input">
					<input type="text" value="女" class="add-inp" v-if="status == 0 &&status == 1&&inpSbm.sex==2" />
					<input type="text" value="男" class="add-inp" v-if="status == 0 &&status == 1&&inpSbm.sex==1" />
					<input type="text" v-if="status != 0 &&status != 1" class="add-inp" disabled="" placeholder="请选择"
						v-model="sexS" />
					<picker v-if="status != 0 &&status != 1" @change="selectSex" range-key="driver_age" :range="sex" :value="index">
						<image src="../../../static/select.png"></image>
					</picker>
				</view> -->
				<view class="input">
					<input type="text" value="女" class="add-inp" v-if="inpSbm != '' &&driver.sex==2" />
					<input type="text" value="男" class="add-inp" v-if="inpSbm != '' &&driver.sex==1" />
					<input type="text" v-if="inpSbm == '' " class="add-inp" disabled="" placeholder="请选择"
						v-model="driver.sexS" />
					<picker v-if="status != 0 &&status != 1" @change="selectSex" range-key="driver_age" :range="sex" :value="index">
						<image src="../../../static/select.png"></image>
					</picker>
				</view>
			</view>
		</view>

		<view class="upload-wrap">
			<view class="upload-tip">请上传您的身份证</view>
			<!-- <view class="upload-box" v-if="inpSbm != ''">
				<view class="upload-item">
					<image :src="inpSbm.id_card_img_1"></image>					
				</view>
				<view class="upload-item">
					<image :src="inpSbm.id_card_img_2"></image>
				</view>
			</view> -->
			<view class="upload-box">
				<view class="upload-item" @click="chooseImages('id_card_img_1')">
					<image src="../../../static/app/upload-bg-1.png" mode="" v-if="driver.id_card_img_1 == ''"></image>
					<view class="upload-icon" v-if="driver.id_card_img_1 == ''">
						<image src="../../../static/app/upload-icon.png" mode="">
					</view>
					<image :src="driver.id_card_img_1" mode="" v-else></image>
				</view>
				<view class="upload-item" @click="chooseImages('id_card_img_2')">
					<image src="../../../static/app/upload-bg-2.png" mode="" v-if="driver.id_card_img_2 == ''"></image>

					<view class="upload-icon" v-if="driver.id_card_img_2 == ''">
						<image src="../../../static/app/upload-icon.png" mode="">
					</view>
					<image :src="driver.id_card_img_2" mode="" v-else></image>
				</view>
			</view>

			<view class="upload-tip">请上传您的行驶证</view>
			<!-- <view class="upload-box" v-if="driver != ''">
				<view class="upload-item">
					<image :src="driver.driver_license_1"></image>				
				</view>
				<view class="upload-item">
					<image :src="driver.driver_license_2"></image>
				</view>
			</view> -->
			<view class="upload-box" >
				<view class="upload-item" @click="chooseImages('driver_license_1')">
					<image src="../../../static/app/upload-bg-7.png" mode="" v-if="driver.driver_license_1 == ''">
					</image>
					<view class="upload-icon" v-if="driver.driver_license_1 == ''">
						<image src="../../../static/app/upload-icon.png" mode="">
					</view>
					<image :src="driver.driver_license_1" mode="" v-else></image>
				</view>
				<view class="upload-item" @click="chooseImages('driver_license_2')">
					<image src="../../../static/app/upload-bg-6.png" mode="" v-if="driver.driver_license_2 == ''">
					</image>
					<image :src="driver.driver_license_2" mode="" v-else></image>
					<view class="upload-icon" v-if="driver.driver_license_2 == ''">
						<image src="../../../static/app/upload-icon.png" mode="">
					</view>
				</view>
			</view>
			<view class="upload-tip">三年无重大事故证明</view>
			<!-- <view class="upload-box" v-if="inpSbm != ''">
				<view class="upload-item">
					<image :src="inpSbm.prove"></image>
					
				</view>
			</view> -->
			<view class="upload-box" >
				<view class="upload-item" @click="chooseImages('prove')">
					<image src="../../../static/prove.png" mode="" v-if="driver.prove == ''"></image>
					<view class="upload-icon" v-if="driver.prove == ''">
						<image class="prove-img1" src="../../../static/app/upload-icon.png" mode="">
					</view>
					<image :src="driver.prove" mode="" v-else></image>
				</view>
			</view>
			<!-- <view class="upload-box d-s-c f-w" v-else>
				<block v-for="(image,index) in driver.prove" :key="index">
					<view class="upload-item">
						<image class="upload-imgs1" :src="image" mode=""></image>
						<view class="icon iconfont icon-guanbi" @tap="delect(index)"></view>
					</view>
				</block>
				<view class="upload-item" @click="chooseImages('driver.prove')">
					<image src="../../../static/app/upload-bg-5.png" mode=""></image>
					<view class="upload-icon">
						<image src="../../../static/app/upload-icon.png" mode="">
					</view>
				</view>
			</view> -->
		</view>
		<view class="auditing" v-if="status ==0">
			<image src="../../../static/inp1.png"></image>
			<text>审核中，请耐心等待审核结果</text>
		</view>
		<view class="auditing" v-if="status ==1">
			<image src="../../../static/inp3.png"></image>
			<text>审核通过</text>
		</view>
		<view class="auditFail" v-if="status ==2">
			<image src="../../../static/inp2.png"></image>
			<text>审核失败，请重新上传</text>
		</view>

		<view style="height: 50rpx;"></view>
		<view class="bottom-wrap" @click="$noMultipleClicks(formSubmit) ">
			<view v-if="status == -1">提交</view>
		</view>
		<!-- <view v-if="inpSbm != ''">
			<view class="inpSbm" v-if="status ==0">已提交</view>
		</view>
		<view class="auditSecc">
			<view v-if="status ==1"></view>
		</view> -->
		<view class="bottom-wrap" @click="$noMultipleClicks(anewSubmit)">
			<view v-if="status ==2">重新上传</view>
		</view>

	</view>
</template>

<script>
	import mpvueCityPicker from '@/components/mpvue-citypicker/mpvueCityPicker.vue';
	var sourceType = [
		['camera'],
		['album'],
		['camera', 'album']
	]
	export default {
		components: {
			mpvueCityPicker
		},
		data() {
			return {
				noClick: true,
				sex: ['男', '女'],
				indexSex: 0,
				// imageList: {
				// 	idcard_pic1: '',
				// 	idcard_pic2: '',
				// 	driving_license1: '',
				// 	driving_license2: '',
				// },
				//car_pic: [],
				cityPickerValueDefault: [0, 0, 0],
				selectCity: '选择省,市,区',
				province_id: '',
				city_id: '',
				region_id: '',
				driverName: '', //司机名字
				driverPhone: '', //司机手机号
				inpSbm: '', //获取已提交的内容
				codeSbm: 1, //提交的状态
				status: -1, //获取提交的状态
				selectType: [], //驾照类型
				yearData: [], //驾龄
				index: '0',
				driver: {
					realname: '', //姓名
					mobile: '', //手机号
					age: '', //年龄
					pay: '', //报酬
					driver_type: '', //驾照类型id
					driver_age: '', //驾龄id
					sex: '', //性别id
					id_card_img_1: '', //身份证正面
					id_card_img_2: '', //身份证反面
					driver_license_1: '', //驾驶证正面
					driver_license_2: '', //驾驶证反面
					prove: '', //无重大事故证明 
					type_name:'',//驾照类型
					age_name:'',//驾龄
					sexS: '', //性别
					driver_id: '', //提交的id
				},
				driverType: '', //驾照类型type_name
				driverAge: '', //驾龄age_name
				sexS: '', //性别
				driver_id: '', //提交的id
			};
		},
		onLoad() {
			//this.getInpmsg();
			this.getType();
			this.getYears();
			this.getInfodata();
		},
		onPullDownRefresh() {
			//this.getInpmsg();
			this.getType();
			this.getYears();
			this.getInfodata();
			setTimeout(() => {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		methods: {
			anewSubmit() {
				let self = this;
				uni.showModal({
					title: '是否要重新上传',
					
					success: function(res) {
						
						//let aa = self.driver;
						// aa.splice(aa.length-3,aa.length);
						//console.log(aa);
						//return;
						if (res.confirm) {
							//self.inpSbm = '';
							//self.status = -1;
							let driver_id = self.driver.driver_id;
							let formdata = {};
							//return;
							// self.judgeInfo();

							// if (!bb.test(this.aa)) {
							// 	uni.showToast({
							// 		title: '姓名有误,请重填！',
							// 		duration: 2000,
							// 		icon: 'none'
							// 	});
							// 	return;
							// }
							let bb = /^[\u4e00-\u9fa5]{0,}$/;
							if (!bb.test(self.driver.realname)) {
								uni.showToast({
									title: '请填写正确的姓名',
									duration: 1000,
									icon: 'none'
								});
								return false;
							}
							if (!/^1(3|4|5|6|7|8|9)\d{9}$/.test(self.driver.mobile)) {
								uni.showToast({
									title: '手机有误,请重填！',
									duration: 2000,
									icon: 'none'
								});
								return;
							}
							if (self.driver.age == '') {
								uni.showToast({
									title: '请输入年龄',
									duration: 1000,
									icon: 'none'
								});
								return false;
							}
							if (self.driver.pay == '') {
								uni.showToast({
									title: '请输入报酬',
									duration: 1000,
									icon: 'none'
								});
								return false;
							}
							if (self.driver.type_name == '') {
								uni.showToast({
									title: '请选择驾照类型',
									duration: 1000,
									icon: 'none'
								});
								return false;
							}
							if (self.driver.age_name == '') {
								uni.showToast({
									title: '请选择驾龄',
									duration: 1000,
									icon: 'none'
								});
								return false;
							}
							if (self.driver.sexS == '') {
								uni.showToast({
									title: '请选择性别',
									duration: 1000,
									icon: 'none'
								});
								return false;
							}
							if (self.driver.id_card_img_1 == '') {
								uni.showToast({
									title: '请上传身份证正面',
									duration: 1000,
									icon: 'none'
								});
								return false;
							}

							if (self.driver.id_card_img_2 == '') {
								uni.showToast({
									title: '请上传身份证反面',
									duration: 1000,
									icon: 'none'
								});
								return false;
							}

							if (self.driver.driver_license_1 == '') {
								uni.showToast({
									title: '请上传您的驾驶证',
									duration: 1000,
									icon: 'none'
								});
								return false;
							}

							if (self.driver.driver_license_2 == '') {
								uni.showToast({
									title: '请上传您的驾驶证',
									duration: 1000,
									icon: 'none'
								});
								return false;
							}

							if (self.driver.prove.length == 0) {
								uni.showToast({
									title: '三年无重大事故证明',
									duration: 1000,
									icon: 'none'
								});
								return false;
							}
							formdata = self.driver;
							uni.showLoading({
								title: '上传中'
							});
							self._post('user.index/add_recruit_driver', 
								{
									...formdata,
									driver_id:driver_id
								},
								function(res) {	
									self.getInfodata();
									uni.hideLoading();
								});
							// self._post(
							// 	'user.index/add_recruit_driver',
							// 	formdata,
							// 	driver_id,
							// 	result => {
							// 		console.log("koooooo")
							// 		if (result.code == 1) {
							// 			uni.showToast({
							// 				title: '成功！',
							// 				duration: 2000,
							// 				icon: 'none'
							// 			});
							// 			console.log("lpkokjbhvgvhbjk")
							// 			//self.onPullDownRefresh();
							// 			self.getInfodata();
							// 			//self.gotoPage('/pages/recruitment/recruitment');
							// 			//self.codeSbm = 2;
							// 			//self.gotoPage('/pages/user/index/index');
							// 		}
							// 	},
							// 	false,
							// 	() => {
							// 		uni.hideLoading();
							// 	}
							// );
						} else if (res.cancel) {
							//console.log('用户点击取消');
						}
					}
				});
				//this.inpSbm = '';
				// this.formSubmit();				
			},
			/* 获取提交的信息 */
			getInfodata() {
				let self = this;
				uni.showLoading({
					title: '加载中'
				});
				self._get('user.index/recruit_driver', {

					},
					function(res) {
						//console.log(res);
						
						if (res.data.Info != null) {
							self.inpSbm = res.data.Info;
							self.driver = res.data.Info;
							self.status = res.data.Info.status;
							self.driver.driver_id = res.data.Info.driver_id;
						}
						//console.log(self.driver.driver_id)
						uni.hideLoading();
					});
			},
			/* 性别选择 */
			selectSex: function(e) {
				this.indexSex = e.target.value;
				this.driver.sexS = this.sex[this.indexSex];
				//console.log(this.indexSex)
				this.driver.sex = this.indexSex + 1;
			},
			/*车辆类型选择 */
			seleceType: function(e) {
				//console.log(e)
				let index = e.detail.value;
				let type = this.selectType[index];
				//console.log(type)
				this.driver.type_name = type.type_name;
				this.driver.driver_type = type.type_id; //传回的id
				this.index = e.detail.value;
			},
			/* 驾龄选择 */
			seleceYears: function(e) {
				let idx = e.detail.value;
				let year = this.yearData[idx];
				this.driver.driver_age = year.id; //传回的id
				this.driver.age_name = year.driver_age;
				//console.log(year.driver_age)
			},
			getYears() {
				let self = this;
				uni.showLoading({
					title: '加载中'
				});
				self._get('car.index/get_driveage', {}, function(res) {
					//console.log(res);
					self.yearData = res.data.list;
					uni.hideLoading();
				});
			},
			getType() {
				let self = this;
				uni.showLoading({
					title: '加载中'
				});
				self._get('home/getdrivertype', {}, function(res) {
					//console.log(res.data.driver_type);
					self.selectType = res.data.driver_type;
					uni.hideLoading();
				});
			},
			chooseImages(type) {
				let self = this;
				// 上传图片
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择
					success: (res) => {
						//console.log(res.tempFilePaths);
						// if( type == 'car_pic' ) {
						// 	self.car_pic = self.car_pic.concat(res.tempFilePaths[0]);
						// } else {
						// 	self.imageList[type] = res.tempFilePaths[0];
						// }

						self.uploadFile(res.tempFilePaths[0], type);
						// if(self.imageList.length == 9) {
						//     self.VideoOfImagesShow = false
						// }
					}
				});
			},
			delect(index) {
				uni.showModal({
					title: "提示",
					content: "是否要删除该图片",
					success: (res) => {
						if (res.confirm) {
							this.driver.prove.splice(index, 1)
						}
					}
				})
			},
			uploadFile: function(filePath, type) {

				let self = this;
				let i = 0;
				// let img_length=tempList.length;
				let params = {
					token: uni.getStorageSync('token'),
					app_id: self.getAppId()
				};
				uni.showLoading({
					title: '图片上传中'
				});
				// tempList.forEach(function(filePath, fileKey) {
				//console.log(filePath);
				uni.uploadFile({
					url: self.websiteUrl + '/index.php?s=/api/file.upload/image',
					filePath: filePath,
					name: 'iFile',
					formData: params,
					success: function(res) {
						let result = typeof res.data === 'object' ? res.data : JSON.parse(res.data);
						if (result.code === 1) {
							let file_path = result.data.file_path;
							if (type == 'driver.prove') {
								self.driver.prove = self.driver.prove.concat(file_path);
							} else {
								self.driver[type] = file_path;
								//console.log(self.driver[type])
							}
							// console.log(result.data.file_path);
							// self.imageList = self.imageList.concat(result.data.file_path);
							// self.imageList.push(result.data);
						}
					},
					complete: function() {
						i++;
						// if (img_length === i) {
						uni.hideLoading();
						// 所有文件上传完成
						// self.$emit('getImgs',self.imageList);
						// }
					}
				});
				// });
			},
			formSubmit() {
				let self = this;
				let formdata = {};
				
				// let bb = /^[\u4e00-\u9fa5]{0,}$/;
				let bb = /^([\u4e00-\u9fa5]{2,20}|([a-zA-Z]+\s?){2,20})$/;
				if (!bb.test(self.driver.realname)) {
					uni.showToast({
						title: '请填写正确的姓名',
						duration: 1000,
						icon: 'none'
					});
					return false;
				}
				if (!/^1(3|4|5|6|7|8|9)\d{9}$/.test(self.driver.mobile)) {
					uni.showToast({
						title: '手机有误,请重填！',
						duration: 2000,
						icon: 'none'
					});
					return;
				}

				if (self.driver.age == '') {
					uni.showToast({
						title: '请输入年龄',
						duration: 1000,
						icon: 'none'
					});
					return false;
				}
				if (self.driver.pay == '') {
					uni.showToast({
						title: '请输入报酬',
						duration: 1000,
						icon: 'none'
					});
					return false;
				}
				if (self.driver.type_name == '') {
					uni.showToast({
						title: '请选择驾照类型',
						duration: 1000,
						icon: 'none'
					});
					return false;
				}
				if (self.driver.age_name == '') {
					uni.showToast({
						title: '请选择驾龄',
						duration: 1000,
						icon: 'none'
					});
					return false;
				}
				if (self.driver.sexS == '') {
					uni.showToast({
						title: '请选择性别',
						duration: 1000,
						icon: 'none'
					});
					return false;
				}
				if (self.driver.id_card_img_1 == '') {
					uni.showToast({
						title: '请上传身份证正面',
						duration: 1000,
						icon: 'none'
					});
					return false;
				}

				if (self.driver.id_card_img_2 == '') {
					uni.showToast({
						title: '请上传身份证反面',
						duration: 1000,
						icon: 'none'
					});
					return false;
				}

				if (self.driver.driver_license_1 == '') {
					uni.showToast({
						title: '请上传您的驾驶证',
						duration: 1000,
						icon: 'none'
					});
					return false;
				}

				if (self.driver.driver_license_2 == '') {
					uni.showToast({
						title: '请上传您的驾驶证',
						duration: 1000,
						icon: 'none'
					});
					return false;
				}


				if (self.driver.prove.length == 0) {
					uni.showToast({
						title: '三年无重大事故证明',
						duration: 1000,
						icon: 'none'
					});
					return false;
				}
				formdata = self.driver;
				//console.log(formdata);
				//return;
				//self.judgeInfo();
				self._post(
					'user.index/add_recruit_driver',
					formdata,
					result => {

						if (result.code == 1) {
							uni.showToast({
								title: '成功！',
								duration: 2000,
								icon: 'none'
							});
							self.getInfodata();
							//self.codeSbm = 2;
							//self.gotoPage('/pages/recruitment/recruitment');
							///self.status = -1;
						}
					},
					false,
					() => {
						uni.hideLoading();
					}
				);
			}
		}
	}
</script>

<style lang="scss">
	@import 'inputMsg.scss';
</style>
